//
//  FirstViewController.m
//  MorePlatformCeShiApp
//
//  Created by dsy123 on 2021/8/10.
//


#define KPushSearchPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"KJPushhggfddd.plist"]


#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,retain) NSMutableArray *totalArray;
@property (nonatomic,retain) NSMutableDictionary *totalDictionAry;

@end

@implementation FirstViewController


- (NSMutableArray *)totalArray {
    if (!_totalArray) {
        _totalArray = [NSMutableArray array];
    }
    return _totalArray;
}
- (NSMutableDictionary *)totalDictionAry {
    if (!_totalDictionAry) {
        _totalDictionAry = [NSMutableDictionary dictionary];
    }
    return _totalDictionAry;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(100, 100, 100, 50);
    [button setTitle:@"点击事件" forState:(UIControlStateNormal)];
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(didClickedButton) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    
    
    
    UIButton *buttonOne = [UIButton buttonWithType:(UIButtonTypeSystem)];
    buttonOne.frame = CGRectMake(100, 200, 100, 50);
    [buttonOne setTitle:@"点击事件海岛大亨" forState:(UIControlStateNormal)];
    buttonOne.backgroundColor = [UIColor blueColor];
    [buttonOne addTarget:self action:@selector(didClickedButtonOne) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:buttonOne];
    
    
    
}
- (void)didClickedButtonOne {
    SecondViewController *vc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didClickedButton {
    _page++;
    NSInteger number = 1;
    if (_page % 2 == 0) {
        number = 2;
    }else {
        number = 1;
    }
    
    //    NSDictionary *userInfo = @{[NSString stringWithFormat:@"%ld",number] : @[@{@"name" : @"aa",
    //                                                                               @"alert" : @"dddd",
    //                                                                               @"platformId" : [NSString stringWithFormat:@"%ld",number]}]};
    NSDictionary *userInfo = @{@"name" : @(_page),
                               @"alert" : @"dddd",
                               @"platformId" : [NSString stringWithFormat:@"%ld",number]};
    
    
//    [self saveUMPushArrayUserInfo:userInfo];
    
    
    [self.totalDictionAry removeAllObjects];
    self.totalDictionAry = [NSKeyedUnarchiver unarchiveObjectWithFile:KPushSearchPath];
    NSMutableArray *listArray = [NSMutableArray array];
    [listArray addObjectsFromArray:[self.totalDictionAry objectForKey:userInfo[@"platformId"]]];
    if (listArray.count > 2) {
        [listArray removeObjectAtIndex:0];
        [listArray addObject:userInfo];
    } else {
        [listArray addObject:userInfo];
    }
    [self.totalDictionAry setObject:listArray forKey:userInfo[@"platformId"]];
    [NSKeyedArchiver archiveRootObject:self.totalDictionAry toFile:KPushSearchPath];
    
//    [[NSFileManager defaultManager] removeItemAtPath:KPushSearchPath error:nil];
    NSLog(@"%@",[NSKeyedUnarchiver unarchiveObjectWithFile:KPushSearchPath]);
    
    
}

//- (void)saveUMPushArrayUserInfo:(NSDictionary *)userInfo {
//    [self.totalArray removeAllObjects];
//    NSMutableDictionary *dictary = [NSMutableDictionary dictionary];
//    NSMutableArray *arrayq = [NSMutableArray array];
//    [self.totalArray addObjectsFromArray:[NSKeyedUnarchiver unarchiveObjectWithFile:KPushSearchPath]];
//    if (self.totalArray.count == 0) {
//        [arrayq addObject:userInfo];
//        [dictary setObject:arrayq forKey:userInfo[@"platformId"]];
//        [self.totalArray addObject:dictary];
//        [NSKeyedArchiver archiveRootObject:self.totalArray toFile:KPushSearchPath];
//        return;
//    }
//
//
//    BOOL isTrue = NO;
//    for (NSDictionary *keyDic in self.totalArray) {
//        NSArray *keyArray = keyDic.allKeys;
//
//        if ([keyArray containsObject:userInfo[@"platformId"]]) {
//            NSMutableArray *array = [NSMutableArray arrayWithArray:keyDic[userInfo[@"platformId"]]];
//            if (array.count > 2) {
//                [array removeObjectAtIndex:0];
//            }
//            [array addObject:userInfo];
//            [dictary setObject:array forKey:userInfo[@"platformId"]];
//            isTrue = YES;
//
//        }
//    }
//    if (!isTrue) {
//
//        [arrayq addObject:userInfo];
//        [dictary setObject:arrayq forKey:userInfo[@"platformId"]];
//    }
//    for (NSInteger i = 0; i < self.totalArray.count;) {
//        NSDictionary *dic = self.totalArray[i];
//        if ([dic.allKeys containsObject:userInfo[@"platformId"]]) {
//            [self.totalArray removeObjectAtIndex:i];
//            break;
//        } else {
//            i++;
//        }
//    }
//    [self.totalArray addObject:dictary];
//    [NSKeyedArchiver archiveRootObject:self.totalArray toFile:KPushSearchPath];
//
//    NSLog(@"%@",[NSKeyedUnarchiver unarchiveObjectWithFile:KPushSearchPath]);
//
//}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
